<?php


namespace app\admin\controller;


use app\BaseController;
use app\lib\Excel;
use app\Request;
use think\facade\Cache;

class Form extends  BaseController
{
    public function getList(Request  $request)
    {

//        $excel = new Excel();
//        //根据此参数获取数据中的值，要与表格标题键名对应
//        $keys = ['name','age'];
//        $excel->exportExcel([['name' => 'alibaba','age' => 18]],$keys,'报名记录' . date('Y-m-d_His') . '.xlsx');
//        exit;
        $branch = $request->post('branch');
        $userInfo = Cache::get(config('cache.redis.prefix') . $request->token);
        $user = \app\admin\model\Manager::field('branch')->find($userInfo['id']);
        $page = $request->post('page', 1);
        $limit = $request->post('limit', 10);
        $model = \app\admin\model\Form::alias('f')->order('f.id desc')
            ->leftJoin('user u', 'f.user_id=u.id')
            ->field('u.wxname,u.wximage,f.*');
        if (!empty($user->branch)) {
            $model->where('branch', $user->branch);
            $total = \app\admin\model\Form::where('branch', $user->branch)->count();
        } else {
            if (!empty($branch)) {
                $model->where('branch', $branch);
                $total = \app\admin\model\Form::where('branch', $branch)->count();
            } else {
                $total = \app\admin\model\Form::count();
            }
        }
        $isExport = $request->post('is_export');
        if (empty($isExport)) {
            $result = $model->page($page, $limit)->select();
            $data = [
                'total' => $total,
                'current' => $page,
                'perpage' => $limit,
                'data' =>  $result
            ];
            return show(config('status.success'), '操作成功', $data);
        } else {
            $result = $model->select()->toArray();
            $excel = new Excel();
            //设置表格标题
            $title = [
                'name' => '姓名',
                'sex' => '性别',
                'department' => '科室',
                'work' => '职务',
                'company' => '公司',
                'branch' => '分会',
                'branch_office' => '分会职务',
                'create_time' => '提交时间',
            ];
            //添加标题到数组头部
            array_unshift($result,$title);

            //根据此参数获取数据中的值，要与表格标题键名对应
            $keys = ['name','sex','department','work', 'company', 'branch', 'branch_office', 'create_time'];
            $excel->exportExcel($result,$keys,'报名记录' . date('Y-m-d_His') . '.xlsx', [
                // 设置宽度
                'setWidth' => [
                    'A' => '20',
                    'B' => '20',
                    'C' => '20',
                    'D' => '30',
                    'E' => '30',
                    'F' => '30',
                    'G' => '30',
                    'H' => '30',
                ]
            ]);
        }
    }

    /**
     * 创建
     * @param Request $request
     * @return \think\response\Json
     */
    public function create(Request $request)
    {
        $name = $request->post('name');
        $sex = $request->post('sex');
        if (mb_strlen($name) < 2) {
            return show(config('status.client_error'), '姓名格式不正确');
        }
        if (!in_array($sex, ['男', '女'])) {
            return show(config('status.client_error'), '性别格式不正确');
        }
        $model = new \app\admin\model\Form();
        $model->name = $name;
        $model->sex = $sex;
        $fields = ['department', 'work', 'company', 'branch', 'branch_id', 'branch_office'];
        foreach ($fields as $field) {
            $model->$field = $request->post($field);
        }

        $model->save();

        return show(config('status.success'), '操作成功');
    }

    /**
     * 更新
     * @param Request $request
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $name = $request->post('name');
        $sex = $request->post('sex');
        $idCard = $request->post('idcard');
        $phone = $request->post('phone');
        if (mb_strlen($name) < 2) {
            return show(config('status.client_error'), '姓名格式不正确');
        }
        if (!in_array($sex, ['男', '女'])) {
            return show(config('status.client_error'), '性别格式不正确');
        }
        $model =  \app\admin\model\Form::find($request->post('id'));
        if (empty($model)) {
            return show(config('status.client_error'), '记录不存在');
        }
        $model->name = $name;
        $model->sex = $sex;
        $model->idcard = $idCard;
        $model->phone = $phone;
        $fields = ['department', 'work', 'company', 'branch', 'branch_id', 'branch_office'];
        foreach ($fields as $field) {
            $model->$field = $request->post($field);
        }

        $model->save();

        return show(config('status.success'), '操作成功');
    }

    public function delete(Request  $request)
    {
        \app\admin\model\Form::destroy($request->post('id'));
        return show(config('status.success'), '操作成功');
    }
}